var criteria = { Name: "Corporate Governance", Level2: [
    { Name: "Board/Management Quality & Integrity Indicators", Level3: [
        { Name: "CG.1.1 Board Experience", Level4: [{ Name: "CG.1.1 Board Experience-Score" }] },
        { Name: "CG.1.2 Director Track Record", Level4: [{ Name: "CG.1.2 Director Track Record-Score" }] },
        { Name: "CG.1.3 Board Capture", Level4: [{ Name: "CG.1.3 Board Capture-Score" }] },
        { Name: "CG.1.4 Votes Against Board", Level4: [{ Name: "CG.1.4 Votes Against Board-Score" }] },
        { Name: "CG.1.5 Responsiveness to Shareholders", Level4: [{ Name: "CG.1.5 Responsiveness to Shareholders-Score" }] },
        { Name: "CG.1.6 Related Party Transactions", Level4: [{ Name: "CG.1.6 Related Party Transactions-Score" }] },
        { Name: "CG.1.7 Director Ownership & Remuneration", Level4: [{ Name: "CG.1.7 Director Ownership & Remuneration-Score" }] },
        { Name: "CG.1.8 Executive/Board Misconduct", Level4: [{ Name: "CG.1.8 Executive/Board Misconduct-Score" }] },
        { Name: "CG.1.9 Business Practices Controversies", Level4: [{ Name: "CG.1.9 Business Practices Controversies-Score" }] },
        { Name: "CG.1.10 Governance Controversies", Level4: [{ Name: "CG.1.10 Governance Controversies-Score" }] }
    ]},
    { Name: "Board Structure Indicators", Level3: [
        { Name: "CG.2.1 Board Leadership", Level4: [{ Name: "CG.2.1 Board Leadership-Score" }] },
        { Name: "CG.2.2 Board Tenure", Level4: [{ Name: "CG.2.2 Board Tenure-Score" }] },
        { Name: "CG.2.3 Board Independence", Level4: [{ Name: "CG.2.3 Board Independence-Score" }] },
        { Name: "CG.2.4 Nominating Committee Effectiveness", Level4: [{ Name: "CG.2.4 Nominating Committee Effectiveness-Score" }] },
        { Name: "CG.2.5 Director Disclosure", Level4: [{ Name: "CG.2.5 Director Disclosure-Score" }] },
        { Name: "CG.2.6 Voting Structures", Level4: [{ Name: "CG.2.6 Voting Structures-Score" }] },
        { Name: "CG.2.7 Directors not Elected by Shareholders", Level4: [{ Name: "CG.2.7 Directors not Elected by Shareholders-Score" }] },
        { Name: "CG.2.8 Risk Oversight", Level4: [{ Name: "CG.2.8 Risk Oversight-Score" }] },
        { Name: "CG.2.9 Board Diversity", Level4: [{ Name: "CG.2.9 Board Diversity-Score" }] },
        { Name: "CG.2.10 Other Board Structural Issues", Level4: [{ Name: "CG.2.10 Other Board Structural Issues-Score" }] }
    ]},
    { Name: "Ownership & Shareholder Rights Indicators", Level3: [
        { Name: "CG.3.1 Classified Board & Director Removal", Level4: [{ Name: "CG.3.1 Classified Board & Director Removal-Score" }] },
        { Name: "CG.3.2 Proportionality � One Share/One Vote", Level4: [{ Name: "CG.3.2 Proportionality � One Share/One Vote-Score" }] },
        { Name: "CG.3.3 Ownership Structure", Level4: [{ Name: "CG.3.3 Ownership Structure-Score" }] },
        { Name: "CG.3.4 Capital Issuance Risks", Level4: [{ Name: "CG.3.4 Capital Issuance Risks-Score" }] },
        { Name: "CG.3.5 Shareholder Action", Level4: [{ Name: "CG.3.5 Shareholder Action-Score" }] },
        { Name: "CG.3.6 Poison Pill & Takeover Defences", Level4: [{ Name: "CG.3.6 Poison Pill & Takeover Defences-Score" }] },
        { Name: "CG.3.7 Supermajority Provisions", Level4: [{ Name: "CG.3.7 Supermajority Provisions-Score" }] },
        { Name: "CG.3.8 Other Shareholder Rights Limitations", Level4: [{ Name: "CG.3.8 Other Shareholder Rights Limitations-Score" }] }
    ]},
    { Name: "Remuneration Indicators", Level3: [
        { Name: "CG.4.1 Remuneration Disclosure", Level4: [{ Name: "CG.4.1 Remuneration Disclosure-Score" }] },
        { Name: "CG.4.2 Remuneration Committee Effectiveness", Level4: [{ Name: "CG.4.2 Remuneration Committee Effectiveness-Score" }] },
        { Name: "CG.4.3 Say on Pay", Level4: [{ Name: "CG.4.3 Say on Pay-Score" }] },
        { Name: "CG.4.4 Pay Controversies", Level4: [{ Name: "CG.4.4 Pay Controversies-Score" }] },
        { Name: "CG.4.5 STI Performance Metrics", Level4: [{ Name: "CG.4.5 STI Performance Metrics-Score" }] },
        { Name: "CG.4.6 LTI Performance Metrics", Level4: [{ Name: "CG.4.6 LTI Performance Metrics-Score" }] },
        { Name: "CG.4.7 Pay Magnitude", Level4: [{ Name: "CG.4.7 Pay Magnitude-Score" }] },
        { Name: "CG.4.8 Pay for Performance", Level4: [{ Name: "CG.4.8 Pay for Performance-Score" }] },
        { Name: "CG.4.9 Pay for Failure", Level4: [{ Name: "CG.4.9 Pay for Failure-Score" }] },
        { Name: "CG.4.10 Equity Ownership Policies", Level4: [{ Name: "CG.4.10 Equity Ownership Policies-Score" }] },
        { Name: "CG.4.11 Internal Pay Equity", Level4: [{ Name: "CG.4.11 Internal Pay Equity-Score" }] },
        { Name: "CG.4.12 Clawback Policy", Level4: [{ Name: "CG.4.12 Clawback Policy-Score" }] },
        { Name: "CG.4.13 Equity Pay Concerns", Level4: [{ Name: "CG.4.13 Equity Pay Concerns-Score" }] }
    ]},
    { Name: "Audit & Financial Reporting Indicators", Level3: [
        { Name: "CG.5.1 Audit Committee Structure", Level4: [{ Name: "CG.5.1 Audit Committee Structure-Score" }] },
        { Name: "CG.5.2 Audit Committee Effectiveness", Level4: [{ Name: "CG.5.2 Audit Committee Effectiveness-Score" }] },
        { Name: "CG.5.3 Auditor Fees", Level4: [{ Name: "CG.5.3 Auditor Fees-Score" }] },
        { Name: "CG.5.4 Audit Rotation Policy", Level4: [{ Name: "CG.5.4 Audit Rotation Policy-Score" }] },
        { Name: "CG.5.5 Auditor Change", Level4: [{ Name: "CG.5.5 Auditor Change-Score" }] },
        { Name: "CG.5.6 Reporting Irregularities", Level4: [{ Name: "CG.5.6 Reporting Irregularities-Score" }] },
        { Name: "CG.5.7 Other Financial Reporting Concerns", Level4: [{ Name: "CG.5.7 Other Financial Reporting Concerns-Score" }] }
    ]},
    { Name: "Stakeholder Governance Indicators", Level3: [
        { Name: "CG.6.1 ESG Governance", Level4: [{ Name: "CG.6.1 ESG Governance-Score" }] },
        { Name: "CG.6.2 ESG Reporting Standards", Level4: [{ Name: "CG.6.2 ESG Reporting Standards-Score" }] },
        { Name: "CG.6.3 Verification of ESG Reporting", Level4: [{ Name: "CG.6.3 Verification of ESG Reporting-Score" }] },
        { Name: "CG.6.4 Global Compact Signatory", Level4: [{ Name: "CG.6.4 Global Compact Signatory-Score" }] },
        { Name: "CG.6.5 ESG Performance Targets", Level4: [{ Name: "CG.6.5 ESG Performance Targets-Score" }] },
        { Name: "CG.6.6 Political Involvement Policy", Level4: [{ Name: "CG.6.6 Political Involvement Policy-Score" }] },
        { Name: "CG.6.7 Lobbying & Political Expenses", Level4: [{ Name: "CG.6.7 Lobbying & Political Expenses-Score" }] },
        { Name: "CG.6.8 Bribery & Corruption Policy", Level4: [{ Name: "CG.6.8 Bribery & Corruption Policy-Score" }] },
        { Name: "CG.6.9 Public Policy Incidents", Level4: [{ Name: "CG.6.9 Public Policy Incidents-Score" }] },
        { Name: "CG.6.10 Environmental Policy", Level4: [{ Name: "CG.6.10 Environmental Policy-Score" }] },
        { Name: "CG.6.11 Tax Evasion Incidents", Level4: [{ Name: "CG.6.11 Tax Evasion Incidents-Score" }] }
    ]}
]};

function uuid() {
    function s4() {
        return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1);
    }
    return s4() + s4() + "-" + s4() + "-" + s4() + "-" + s4() + "-" + s4() + s4() + s4();
}

function HexToBase64(hex) {
    var base64Digits = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    var base64 = "";
    var group;
    for (var i = 0; i < 30; i += 6) {
        group = parseInt(hex.substr(i, 6), 16);
        base64 += base64Digits[(group >> 18) & 0x3f];
        base64 += base64Digits[(group >> 12) & 0x3f];
        base64 += base64Digits[(group >> 6) & 0x3f];
        base64 += base64Digits[group & 0x3f];
    }
    group = parseInt(hex.substr(30, 2), 16);
    base64 += base64Digits[(group >> 2) & 0x3f];
    base64 += base64Digits[(group << 4) & 0x3f];
    base64 += "==";
    return base64;
}

function NUUID(uuid) {
    var hex = uuid.replace(/[{}-]/g, ""); // remove extra characters
    var a = hex.substr(6, 2) + hex.substr(4, 2) + hex.substr(2, 2) + hex.substr(0, 2);
    var b = hex.substr(10, 2) + hex.substr(8, 2);
    var c = hex.substr(14, 2) + hex.substr(12, 2);
    var d = hex.substr(16, 16);
    hex = a + b + c + d;
    var base64 = HexToBase64(hex);
    return new BinData(3, base64);
}

function processLevel1(criteria) {
    if (!db.CriteriaLevel1.findOne({ Name: criteria.Name })) {
        db.CriteriaLevel1.insert({
            _id: NUUID(uuid()),
            Name: criteria.Name,
            ShortName: criteria.Name,
            Order: 25
        });
    }

    var criteriaLevel1Id = db.CriteriaLevel1.findOne({ Name: criteria.Name })._id;

    criteria.Level2.forEach(function (criteria, index) {
        processLevel2(criteriaLevel1Id, criteria, index);
    });
}

function processLevel2(criteriaLevel1Id, criteria, index) {
    if (!db.CriteriaLevel2.findOne({ Name: criteria.Name })) {
        db.CriteriaLevel2.insert({
            _id: NUUID(uuid()),
            CriteriaLevel1Id: criteriaLevel1Id,
            UniverseFeature: 15,
            Name: criteria.Name,
            ShortName: 'GovIndicators',
            Order: index
        });
    }

    var criteriaLevel2Id = db.CriteriaLevel2.findOne({ Name: criteria.Name })._id;
    var length = criteria.Level3.length;

    criteria.Level3.forEach(function (criteria, index) {
        processLevel3(criteriaLevel2Id, criteria, length - index);
    });
}

function processLevel3(criteriaLevel2Id, criteria, index) {
    if (!db.CriteriaLevel3.findOne({ Name: criteria.Name })) {
        db.CriteriaLevel3.insert({
            _id: NUUID(uuid()),
            Name: criteria.Name,
            ShortName: criteria.Name,
            Order: index,
            CriteriaLevel2Id: criteriaLevel2Id,
            Category: "None",
        });
    }

    var criteriaLevel3Id = db.CriteriaLevel3.findOne({ Name: criteria.Name })._id;

    criteria.Level4.forEach(function (criteria) {
        processLevel4(criteriaLevel2Id, criteriaLevel3Id, criteria, index);
    });
}

function processLevel4(criteriaLevel2Id, criteriaLevel3Id, criteria, index) {
    if (!db.OldTemplateCriteria.findOne({ Name: criteria.Name, CriteriaLevel2Id: criteriaLevel2Id })) {
        db.OldTemplateCriteria.insert({
            _id: NUUID(uuid()),
            Name: criteria.Name,
            CriteriaEntity: "ScreeningData",
            CriteriaEntityField: criteria.Name.split('.').join(''),
            Score: null,
            TemplateIndicatorNumber: null,
            CriteriaLevel2Id: criteriaLevel2Id,
            CriteriaType: 17,
            FilterId: null,
            CompaniesIdsResult: null,
            Order: index
        });
    }

    if (!db.TemplateCriteria.findOne({ Name: criteria.Name, CriteriaLevel2Id: criteriaLevel2Id })) {
        db.TemplateCriteria.insert({
            _id: NUUID(uuid()),
            Name: criteria.Name,
            CriteriaEntity: "ScreeningData",
            CriteriaEntityField: criteria.Name.split('.').join(''),
            CriteriaType: 17,
            CriteriaLevel2Id: criteriaLevel2Id,
            CriteriaLevel3Id: criteriaLevel3Id,
            IsScreenable: true
        });
    }
}

use ClientPlatform

db.CriteriaLevel1.update({ Order: { $lt: 10 } }, { $mul: { Order: 10 } }, { multi: true });

processLevel1(criteria);
